<template>
  <div class="myContainer">
    <el-row class="myTitleBorderBottom myMarginBottom_10px">
      <el-col :span="12" class="myLineHeight_40px">
        {{title}}
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         所属小区:
      </el-col>
      <el-col :span="7">
        <template>
          <el-cascader
            placeholder="请选择小区"
            :options="regionOptions"
            v-model="regionOptSelected"
            @change="changeArea"
            ></el-cascader>
          </el-select>
        </template>
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         工单类型:
      </el-col>
      <el-col :span="7">
        <template>
          <el-select v-model="worklistType" clearable placeholder="请选择工单类型" @change="changeType">
            <el-option
              v-for="item in worklistOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         类别:
      </el-col>
      <el-col :span="7">
        <template>
          <el-select v-model="worklistClass" clearable placeholder="请选择类别">
            <el-option
              v-for="item in worklistclassOptions"
              :key="item.affair_subtype_id"
              :label="item.subtype_name"
              :value="item.affair_subtype_id">
            </el-option>
          </el-select>
        </template>
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         上报人类型:
      </el-col>
      <el-col :span="7">
        <template>
          <el-select v-model="popType" clearable placeholder="请选择上报人类型" @change="changePoptype">
            <el-option
              v-for="item in popOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         上报人:
      </el-col>
      <el-col :span="7">
        <el-input placeholder="请选择上报人" v-model="sbName" @focus="onGetPersonLiable" :disabled="disabled"></el-input>
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         联系电话:
      </el-col>
      <el-col :span="7">
        <el-input placeholder="请输入联系电话" v-model="phone" disabled></el-input>
      </el-col>
    </el-row> 
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         联系地址:
      </el-col>
      <el-col :span="7">
        <el-input placeholder="请输入联系地址" v-model="address"></el-input>
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        <span class="require myVA_M">*</span>
         问题描述:
      </el-col>
      <el-col :span="7">
        <el-input placeholder="请输入问题描述" type="textarea" v-model="issue"></el-input>
      </el-col>
    </el-row>
    <el-row class="myPaddingBottom_10px">
      <el-col :span="7" :offset="3"> 
        <el-button type="primary" class="myBgGreen" @click="onCreate">新 增</el-button>
        <el-button @click="onBack">返 回</el-button>
      </el-col>
    </el-row>

    <!--选择上报人弹出框 start-->
    <el-dialog title="选择上报人" class="myRoleDialog" :visible.sync="personLiableDialog" >
      <el-row :gutter="10">
        <el-col :span="20">
          <el-input placeholder="输入姓名进行检索" v-model="r_kw"></el-input>
        </el-col>
        <el-col :span="4" class="myTextAlignRight">
          <el-button type="primary" class="myBgGreen" @click="onSearchExaminer">查询</el-button>
        </el-col>
      </el-row>
      <div class="myPaddingTop_10px">
        <el-tree
          class="myOV_y_400px" 
          ref="dutyDlg"
          v-loading="dutyDlgListLoading"
          node-key="user_id"
          :data="dutyDlgList"
          :props="dutyDlgProps" 
          @check-change="onChange"
          :render-content="renderContent"
          show-checkbox>
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="personLiableDialog = false">取 消</el-button>
        <el-button type="primary" class="myBgGreen" @click="onAssignSub">提 交</el-button>
      </div>
    </el-dialog>
    <!--选择上报人弹出框 end-->
  </div>
</template>
<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
    padding-right: 10px;
  }
  .myPaddingBottom_10px{
    padding-bottom: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .myMarginBottom_10px{
    margin-bottom: 20px;
  }
  .myOV_y_400px{
    max-height: 400px;
    overflow-y:scroll;
    border:1px solid #f2f2f2; 
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }
</style>
<script>

  import { getRegion } from '@/utils/localstorage';
  import { extractAreaIds } from '@/utils/misc';
  import { getStaffList , getUserInfo } from '@/api/staff'; 
  import { addAffair , getSubtypeList } from '@/api/affair_setting';
  export default {
    data(){
      return{
        title: "新增工单",
        worklistType:"",
        worklistOptions:[
          {label:"报修类",value:"1"},
          {label:"报事类",value:"2"},
          {label:"投诉类",value:"3"}
        ],
        worklistClass:"",
        worklistclassOptions:[],
        popType:"",
        popOptions:[
          {label:"员工",value:"2"},
          {label:"业主",value:"1"}
        ],
        phone:"",
        address:"",
        issue:"",
        regionOptions: [],
        regionOptSelected: [],
        RoleDataProps:{},
        personLiableDialog:false,
        executivePersonDialog:false,
        name:"",
        count:"",
        charge_userid:"",
        note:"",
        dutyDlgList:[],
        dutyDlgProps: {
          children: 'children',
          label: 'user_name'
        },
        dutyDlgListLoading:false,
        personLiable:{},
        epName:"",
        epId:"",
        planid:"",
        r_kw:"",
        i:0,
        disabled:true,
        sb_userid:"",
        sbName:""
      }
    },
    components: {
        
    },
    watch: { 
      popType(val){
        if(val){
          this.disabled=false;
        }else{
          this.disabled=true;
        }
      }
    },
    computed: {
      areaIds() {
        return extractAreaIds(this.regionOptSelected, this.regionOptions);
      }
    },
    mounted(){
      document.title = this.title;
      this.regionOptions = getRegion();
      this.regionOptSelected.push(this.regionOptions[0].value,this.regionOptions[0].children[0].value,this.regionOptions[0].children[0].children[0].value);
    },
    created(){
      document.title=this.title; 
      
    },
    methods:{   
      onBack(){//返回
        this.$router.go(-1);
      },
      onGetPersonLiable(){//指派上报人
        let vm=this;
        if(vm.areaIds && vm.popType){
          vm.r_kw="";
          vm.fetchStaff();
          vm.personLiableDialog = true;
        }        
      },
      handlePersonLiableClick(){

      },
      onGetExecutivePerson(){
        this.r_kw="";
        this.fetchStaff();
        this.executivePersonDialog=true;
      }, 
      fetchStaff() {//获取人员列表
        let vm = this;
        vm.dutyDlgListLoading = true;
        let params = {
          keyword:vm.r_kw,
          area_id:vm.areaIds,
          type:vm.popType
        };
        getUserInfo(params)
        .then(response => {
          vm.dutyDlgList = response.data;
          vm.dutyDlgListLoading = false;
        })
        .catch(error => {
          vm.dutyDlgListLoading = false;
        })
      },
      onChange(a,b,c){
        let vm=this; 
        vm.i++;
        if(vm.i%2==0){
          if(b){
            vm.$refs.dutyDlg.setCheckedKeys([]);
            vm.$refs.dutyDlg.setCheckedKeys([a.user_id]);
          }else{
            vm.$refs.dutyDlg.setCheckedKeys([]);
          }
        }
      },
      renderContent:function(createElement, { node, data, store }) {
        var vm = this;
        return createElement(
          'div', 
          {
            attrs:{
              style:"width:100%",
            }
          },
          [
            createElement('span', node.data.user_name),
            createElement('span', 
              {attrs:{
                style:"float: right; margin-right: 20px"
              }},
              `${node.data.desc}`
            )
          ]
        );
      },
      onAssignSub(){//确认上报人
        let vm=this;
        var pl = vm.$refs.dutyDlg.getCheckedNodes();
        if (pl.length > 0) {
          vm.sb_userid=pl[0].user_id;
          vm.sbName = pl[0].user_name;
          vm.phone = pl[0].phone;
        }
        vm.personLiableDialog=false;
      }, 
      onSearchExaminer(){
        this.fetchStaff();
      }, 
      changeArea(){
        this.sb_userid="";
        this.phone="";
        this.sb_userid="";
        this.sbName="";
        this.phone="";
      },
      onCreate(){
        let vm=this;
        let params={
          area_id:vm.areaIds,
          affair_type:vm.worklistType,
          affair_subtype:vm.worklistClass,
          user_id:vm.sb_userid,
          addr:vm.address,
          note:vm.issue,
          commit_user_type:vm.popType
        }
        addAffair(params).then(response => {
          let res=response.data;
          vm.$message.success("执行成功！");
          vm.onBack();
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      changeType(val){ 
        let vm=this;
        vm.worklistClass="";
        let params={ 
          page_size:9999,
          page_num:1,
          affair_type:val
        }
        getSubtypeList(params).then(response => {
          let res=response.data;
          vm.worklistclassOptions=res.rows; 
        }).catch(error => {})
      },
      changePoptype(){
        let vm=this;
        vm.i=0;
        vm.sb_userid="";
        vm.sbName="";
        vm.phone="";
      }
    }
  
  }


</script>



